/*
 * This file is part of the Carpet AMS Addition project, licensed under the
 * GNU Lesser General Public License v3.0
 *
 * Copyright (C) 2025 A Minecraft Server and contributors
 *
 * Carpet AMS Addition is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Carpet AMS Addition is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Carpet AMS Addition. If not, see <https://www.gnu.org/licenses/>.
 */

package club.mcams.carpet.network.payloads.handshake;

import club.mcams.carpet.AmsClient;
import club.mcams.carpet.network.AMS_CustomPayload;
import club.mcams.carpet.network.AMS_PayloadManager;
import club.mcams.carpet.utils.MinecraftClientUtil;
import club.mcams.carpet.utils.NetworkUtil;
import club.mcams.carpet.utils.Noop;

import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.network.PacketByteBuf;

import java.util.UUID;

public class RequestHandShakeS2CPayload extends AMS_CustomPayload {
    private static final String ID = AMS_PayloadManager.PacketId.REQUEST_HANDSHAKE_S2C.getId();

    private RequestHandShakeS2CPayload() {
        super(ID);
    }

    private RequestHandShakeS2CPayload(PacketByteBuf buf) {
        super(ID);
    }

    @Override
    protected void writeData(PacketByteBuf buf) {
        Noop.noop();
    }

    @Override
    public void handle() {
        NetworkUtil.executeOnClientThread(() -> {
            ClientPlayerEntity player = MinecraftClientUtil.getCurrentPlayer();
            String modVersion = AmsClient.getVersion();
            UUID uuid = player.getUuid();
            NetworkUtil.sendC2SPacket(player, HandShakeC2SPayload.create(modVersion, uuid));
        });
    }

    public static RequestHandShakeS2CPayload create() {
        return new RequestHandShakeS2CPayload();
    }

    public static void register() {
        AMS_PayloadManager.register(ID, RequestHandShakeS2CPayload::new);
    }
}
